<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="800"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{ top: '93px', left: '154px' }"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="取消"
      okText="保存"
      style="top: 15%; height: 55%"
    >

      <a-spin :spinning="confirmLoading">
        <a-form :form="form" id="accountModal">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
            <a-input placeholder="请输入名称" v-decorator.trim="['accountName', validatorRules.accountName]" />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="编号">
            <a-input placeholder="请输入编号" v-decorator.trim="['accountNo']" />
          </a-form-item>
           <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="帐户类型">
             <a-select
              placeholder="选择帐户类型"
              v-decorator="['accountType', validatorRules.accountType]"
              :dropdownMatchSelectWidth="false"
            >
              <a-select-option v-for="(item, index) in accountType" :key="index" :value="item">
                {{ item }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="期初金额">
            <a-input placeholder="请输入期初金额" v-decorator.trim="['initialBalance']" />
          </a-form-item>
         
         <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
<a-switch default-checked v-model="status" />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否默认">
<a-switch default-checked v-model="isDefault" />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
            <a-textarea :rows="2" placeholder="请输入备注" v-decorator="['remark']" />
          </a-form-item>
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>
<script>
import pick from 'lodash.pick'
import { addAccount, editAccount, checkAccount,getAccountType } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
import { mixinDevice } from '@/utils/mixin'
export default {
  name: 'AccountModal',
  mixins: [mixinDevice],
  data() {
    return {
      accountType:[],
      isDefault:false,
      status:true,
      title: '操作',
      visible: false,
      model: {},
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        accountName: {
          rules: [
            { required: true, message: '请输入名称!' },
            { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
          ],
        },
        accountType: {
          rules: [
            { required: true, message: '请输入名称!' },
          ],
        },
      },
    }
  },
  created() {},
  mounted() {
    getAccountType().then((res) =>{
this.accountType=res.data
    })
  },
  methods: {
    add() {

      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'accountName',
            'accountNo',
            'accountType',
            'initialBalance',
            'remark',
            'status',
            'isDefaultAccount'
          )
        )
        autoJumpNextInput('accountModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(this.model, values)
          let obj
          formData.status = this.status ? 1 : 0
          formData.isDefaultAccount = this.isDefault ? 1 : 0
          if (!this.model.id) {
            obj = addAccount(formData)
          } else {
            obj = editAccount(formData)
          }
          obj
            .then((res) => {
              if (res.code === 200) {
                that.$emit('ok')
              } else {
                that.$message.warning(res.msg)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateAccountName(rule, value, callback) {
      let params = {
        name: value,
        id: this.model.id ? this.model.id : 0,
      }
      checkAccount(params).then((res) => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    },
  },
}
</script>
<style scoped>
</style>